package com.hh.commonutils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import java.util.HashMap;
import java.util.Map;

@Data
@Accessors(chain = true)
public class Result{
    @ApiModelProperty(value = "是否成功")
    private Boolean success;

    @ApiModelProperty(value = "状态码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String msg;

    @ApiModelProperty(value = "返回数据")
    private Map<String, Object> data = new HashMap<>();

    private Result(){}

    public static Result success(){
        Result result = new Result();
        result.setSuccess(true)
                .setCode(ResultCode.SUCCESS)
                .setMsg("成功!");
        return result;
    }

    public static Result fail(){
        Result result = new Result();
        result.setSuccess(false)
                .setCode(ResultCode.ERROR)
                .setMsg("失败!");
        return result;
    }

    //失败静态方法
    public static Result error() {
        Result r = new Result();
        r.setSuccess(false);
        r.setCode(ResultCode.ERROR);
        r.setMsg("失败");
        return r;
    }

    public Result success(Boolean success){
        this.setSuccess(success);
        return this;
    }

    public Result data(String key, Object value){
        this.data.put(key, value);
        return this;
    }

    public Result data(Map<String, Object> map){
        this.setData(map);
        return this;
    }

}
